package com.caiwangdai.app.ui.home.repay.list

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.caiwangdai.app.R
import com.caiwangdai.app.bean.RepaymentB
import com.caiwangdai.app.extension.*
import com.caiwangdai.app.origin.list.BaseRecyclerAdapter
import com.caiwangdai.app.origin.list.holder.BaseRecyclerViewHolder
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter
import kotlinx.android.synthetic.main.list_header_repay.view.*
import kotlinx.android.synthetic.main.list_item_repay.view.*

class BorrowAdapter : BaseRecyclerAdapter<RepaymentB>, StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {

    var onRepayListener: ((orderId: String) -> Unit)? = null//还款

    constructor(mContext: Context) : super(mContext)

    //Header ----
    inner class HeaderViewHolder(itemView: View?) : BaseRecyclerViewHolder(itemView)

    override fun getHeaderId(position: Int): Long = getItem(position)?.repay_date?.toLong()!!//headerID
    override fun onCreateHeaderViewHolder(parent: ViewGroup?) = HeaderViewHolder(parent?.inflate(R.layout.list_header_repay))
    override fun onBindHeaderViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        val holder = holder as HeaderViewHolder
        val itemObj = getItem(position)
        holder.contentView.tv_header_date.text = itemObj?.repay_date
    }


    //Item ----
    inner class ItemViewHolder(itemView: View?) : BaseRecyclerViewHolder(itemView)

    override fun getItemLayoutRes() = R.layout.list_item_repay
    override fun getViewHolder(picNewsView: View?) = ItemViewHolder(picNewsView)
    override fun ItemViewChange(holder: BaseRecyclerViewHolder?, position: Int) {
        val holder = holder as ItemViewHolder
        val itemObj = getItem(position)

        with(holder.contentView) {
            tv_date.text = itemObj?.repay_date
            tv_money.text = itemObj?.repay_money
            tv_status.text = itemObj?.status_msg

            if (itemObj?.repay_button == "1") tv_repay.visible() else tv_repay.gone()

            tv_repay.setOnClickListenerPro {
                onRepayListener?.invoke(itemObj?.order_id!!)
            }


            when (itemObj?.status_code) {
                0 -> {
                    //审核中
                    tv_status.setTextColor(mContext.getColorWM(R.color.record_status_verifying))
                    tv_status.setBackgroundResource(R.drawable.bg_circle_green_light)
                }
                1 -> {
                    //已放款
                    tv_status.setTextColor(mContext.getColorWM(R.color.record_status_loan))
                    tv_status.setBackgroundResource(R.drawable.bg_circle_orange_light)
                }
                2 -> {
                    //已还款
                    tv_status.setTextColor(mContext.getColorWM(R.color.record_status_repay))
                    tv_status.setBackgroundResource(R.drawable.bg_circle_blue_light)
                }
                3 -> {
                    //逾期
                    tv_status.setTextColor(mContext.getColorWM(R.color.record_status_overdue))
                    tv_status.setBackgroundResource(R.drawable.bg_circle_red_light)
                }
                4 -> {
                    //未通过
                }
            }
        }
    }


}
